#ifdef HERZELEID_DESKTOP
 #if _MSC_VER
  #pragma once
 #endif

 #ifndef __HERZELEID_COREDISPATCHER_INCLUDED__
  #define __HERZELEID_COREDISPATCHER_INCLUDED__
  #include "HerzeleidCoreDispatcherMessageHandler.hxx"
  #include "HerzeleidCoreDispatcherClosingOption.hxx"
  #include "HerzeleidCoreProcessEventsOption.hxx"
  #include "HerzeleidCoreWindowMessage.hxx"
  #include "HerzeleidCoreWindowInc.hxx"
  #include "HerzeleidRect.hxx"

// Provides the core event message dispatcher. Instances of this type are responsible for processing the window messages and dispatching the events to the client.
HerzeleidUICoreNamespaceBegin class CoreDispatcher : public ICoreDispatcherMessageHandler
{
	friend class CoreWindow;
protected:		

	typedef std::exception Error;															// Represents an standard exception.
	typedef Concurrency::task<Bool> AsyncAction;											// Represents an asynchronous cancellable action.
	typedef Concurrency::task_canceled AsyncActionCanceledError;							// Represents an asynchronous task cancelled exception.
	typedef Concurrency::cancellation_token_source AsyncCancellationTokenSource;			// Represents an asynchronous cancellation token source.
	typedef Concurrency::cancellation_token AsyncCancellationToken;							// Represents an asynchronous cancellation token.
	typedef Concurrency::concurrent_queue<WindowMessage> WindowMessageQueue;				// Raw message queue shortcut.
	typedef HerzeleidUICoreNamespaceImpl::WindowMessage WindowMessage;						// Window message raw description.
	typedef HerzeleidFoundationNamespaceImpl::Rect Rect;									// Rectangle type shortcut.
	TinyTypeDecl( AsyncActionCanceledError );												// Represents an asynchronous task cancelled exception.
	TinyTypeDecl( WindowMessageQueue );														// Raw window captured message queue.
	TinyTypeDecl( WindowMessage );															// Raw window captured message.
	TinyTypeDecl( Error );																	// Standard exception shortcuts.
	TinyTypeDecl( Rect );																	// Rectangle type shortcut.

	AsyncAction m_MsgCapturing;												// Asynchronous queue chain modifications. 
	AsyncAction m_MsgDispatching;											// Event chain handling. 
	WindowMessageQueue m_Messages;											// Captured window raw messages. 
	AsyncCancellationTokenSource m_CancellationTokenSrcForCapturing;		// Cancellation for capturing task chain.
	AsyncCancellationTokenSource m_CancellationTokenSrcForDispatching;		// Cancellation for dispatching task chain.
	CoreDispatcherClosingOption m_eClosingOption;							// Indicates the action should be executed when closing the dispatcher instance. 
	CoreWindowPtr m_HostReferencePtr;										// The host window reference.
	Bool m_bIgnoreProcessRequests;											// Indicates whether user ProcessEvents requests should be ignored. 
	Bool m_bPointerExitedClientRect;										// Indicates whether user ProcessEvents requests should be ignored. 
	Bool m_bWindowWasMinimized;												// Indicates whether user ProcessEvents requests should be ignored. 
	Rect m_BoundingRectBuffer;												// Indicates whether user ProcessEvents requests should be ignored. 

	// Constructs CoreWindow events dispatcher (FRIENDS ONLY). 
	CoreDispatcher( _In_ CoreWindowPtrConst HostReferencePtr );				// As internal function since the dispatcher object can only by used/created through the CoreWindow instance.		

protected:

	// Processing the input queue for this instance of CoreWindow.
	virtual LResultConst WindowProc(
		_In_ WindowHandleConst hWnd,		// Window handle
		_In_ UInt32Const uiMsg,			// Window message
		_In_ WParamConst wParam,			// First parameter (historically word parameter)
		_In_ LParamConst lParam			// Second parameter (historically long parameter)
		) override;

public:

	virtual ~CoreDispatcher();

	// NOT FULLY IMPLIMENTED: Starts the dispatcher processing the input event queue for this instance of CoreWindow.
	void ProcessEvents( 
		_In_ CoreProcessEventsOptionConstRef eOption // The options for processing window events.
		);

private:

	// Starts the dispatcher processing the signle input event for this instance of CoreWindow (INTERNAL/SPECIFIC). 
	void Process( _In_ WindowMessageConstRef Msg );

	// Restarts all the internal task chains (INTERNAL/SPECIFIC). 
	void RestartAll();
	// Cancels all the internal task chains (INTERNAL/SPECIFIC). 
	void DiscardAll();

} HerzeleidUICoreNamespaceEnd; 

HerzeleidUICoreNamespaceBegin
	typedef CoreDispatcher CoreWindowEventHandler;
HerzeleidUICoreNamespaceEnd;

 #endif // !HERZELEID_COREDISPATCHER_INCLUDED__
#endif // !HERZELEID_DESKTOP
